<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"
	import="com.total.reporting.dao.Item,com.total.reporting.presentation.*,java.util.*"%>
<%@ include file="accessControl.jsp"%>

<jsp:useBean id="item" class="com.total.reporting.dao.Item" scope="page"></jsp:useBean>
	
<% 
String id = request.getParameter("id");
if (id == null) 	
	id = (String) request.getAttribute("id");
if(id==null){
	return;
}
String str=request.getParameter("deleteItem");
if (request.getParameter("deleteItem") != null && request.getParameter("deleteItem").equals("true")) {
	request.setAttribute("itemIdToRemoveFromTree", id);
	
	ItemController.deleteItemById(id);
	
	request.getRequestDispatcher("start.jsp").forward(request, response);
	return;
}
if (request.getParameter("ok") != null) {
	if(!id.equals("-1")){
		item=ItemController.getItemById(id);
	}
	item.setDateModification(new java.util.Date());
	item.setDateCreation(new java.util.Date());
	item.setName(request.getParameter("name"));
	item.setDescription(request.getParameter("description"));
	item.setString1(request.getParameter("string1"));
	item.setString2(request.getParameter("string2"));
	item.setString3(request.getParameter("string3"));
	item.setString4(request.getParameter("string4"));
	String param=request.getParameter("float1");
	if(param!=null && param.length()>0)
		item.setAfloat1(Float.parseFloat(param));
	param=request.getParameter("float2");
	if(param!=null && param.length()>0)
	item.setAfloat2(Float.parseFloat(param));
	param=request.getParameter("float3");
	if(param!=null && param.length()>0)
	item.setAfloat3(Float.parseFloat(param));
	param=request.getParameter("float4");
	if(param!=null && param.length()>0)
	item.setAfloat4(Float.parseFloat(param));
	param=request.getParameter("int1");
	if(param!=null && param.length()>0)
	item.setAint1(Integer.parseInt(param));
	param=request.getParameter("int2");
	if(param!=null && param.length()>0)
	item.setAint2(Integer.parseInt(param));
	param=request.getParameter("int3");
	if(param!=null && param.length()>0)
	item.setAint3(Integer.parseInt(param));
	param=request.getParameter("int4");
	if(param!=null && param.length()>0)
	item.setAint4(Integer.parseInt(param));
	param=request.getParameter("date1");
	if(param!=null && param.length()>0)
		item.setAdate1(Util.getDateFromString(param));
	param=request.getParameter("date2");
	if(param!=null && param.length()>0)
		item.setAdate2(Util.getDateFromString(param));


// Manque les dates!!
	
	
	
	//item.setCreatedBy(user.getPrenom()+" "+user.getNom());
	if(id.equals("-1")){
	//	item=new Item();
		item.setType(request.getParameter("type"));
		item.setParentId(Integer.parseInt(request.getParameter("parentId")));
		int id2=ItemController.saveItem(item);
		item.setId(id2);
		request.setAttribute("id", ""+id2);
		request.setAttribute("addToTree",item);
	}
	else{
		item.setId(Integer.parseInt(id));
		ItemController.updateItem(item);
		request.setAttribute("id", id);
		
	}
	//System.out.println (request.getRequestDispatcher("itemRead.jsp"));
		request.getRequestDispatcher("itemRead.jsp?").forward(request, response);
	return;
} else {



}
Map<String,String> treeIconsMap=(Map<String,String>) application.getAttribute("treeOpenedIconsMap");
String itemIcon=null;
String type=null;
if(id.equals("-1")){//new
	String parentType= request.getParameter("parentType");
	String parentId= request.getParameter("parentId");
	if(parentType!=null)
		type=ConceptController.getFirstChildConceptName(parentType);
	else
		type=request.getParameter("type");
	
	
	
	item= new Item();
	if(type.equalsIgnoreCase("rapport")){
		int week = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
		int year = Calendar.getInstance().get(Calendar.YEAR);
		item.setName("Semaine "+week+" "+year);
	}
	
	item.setType(type);
	item.setParentId(Integer.parseInt(parentId));
	item.setDateModification(new java.util.Date());
	//item.setCreatedBy(user.getPrenom()+" "+user.getNom());
	
	
}
else{
	item = ItemController.getItemById(id);
	type=item.getType();
}

 itemIcon=treeIconsMap.get(type);
if(itemIcon==null)
	itemIcon="";
else
	itemIcon="<img width='30px' align='middle' style='vertical-align:middle;'  src='images/"+itemIcon+"'>";


String includePage="_"+item.getType().toLowerCase()+"Write.jsp";
request.setAttribute("item",item);



%>





<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title><%=type %></title>
<LINK rel="stylesheet" type="text/css" href="css/redmond/jquery-ui-1.8.14.custom.css">
<LINK rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.datepicker-fr.js" ></script>	
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js" ></script >
<script type="text/javascript" src="js/control.js" ></script >
<script type="text/javascript" >
tinyMCE.init({
        mode : "textareas",
        theme : "advanced" ,//(n.b. no trailing comma, this will be critical as you experiment later)
            plugins : "emotions,spellchecker,advhr,insertdatetime,preview", 
            
            // Theme options - button# indicated the row# only
            theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect",
            theme_advanced_buttons2 : "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor,|,insertdate,inserttime,|,spellchecker,advhr,removeformat,emotions",
           // theme_advanced_buttons3 : "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions",      
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
           // theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true,
            content_css : "css/custom_content.css",
            theme_advanced_font_sizes: "10px,12px,13px,14px,16px,18px,20px",
            font_size_style_values : "10px,12px,13px,14px,16px,18px,20px"
            
});
</script >

<script type="text/javascript">

function getUserByIgg(id){
      window.location.href="userDetail.jsp?igg="+id;
}

function confirmDelete2(){
	if(parent.d.hasChildren(<%=id%>)){
		alert( "A node with children cannot be deleted. Delete All children first");
		return;
	}
     if (confirm("confirm deletion")){
     	document.forms[0].deleteItem.value="true";
    	 document.forms[0].submit();
     }
}

</script>

</head>

<body>
<div class="detail">
<form   onsubmit="return Verif_Forms(this);"><input type="hidden" name="id" value="<%=id%>">
<input type="hidden" name="deleteItem" value="false"/>
<input type="hidden" name="type" value="<%=item.getType()%>"/>
<input type="hidden" name="parentId" value="<%=item.getParentId()%>"/>
&nbsp;
<%
	if (userRole.equals("admin") || true) {
%>
<input type="submit" name="ok" value="Sauver"/>&nbsp;<input type="button" name="delete"  value="Supprimer" onclick="confirmDelete2()"/>
<%
	}
%>
<div>

<table>

	<tr>

		<td class="label"><span class="title"><%=itemIcon%>&nbsp;<%=item.getType()%></span></td>
		<td><input name="name" id="ot_name" size="50" value="<%=item.getName()%>"/></td>
	</tr>
	
	<jsp:include page="<%=includePage%>"></jsp:include>
	
	<tr>

		<td class="label">description</td>

		<td><textarea  name="description" class="html-editor" cols="80" rows="20"><%=item.getDescription()%></textarea></td>

	</tr>






</table>

</div>
<%
	if (userRole.equals("admin") || true) {
%>
<input type="submit" name="ok" value="Sauver"/>&nbsp;<input type="button" name="delete2"  value="Supprimer" onclick="confirmDelete2()"/>
<%
	}
%>
</form>
</div>
</body>

</html>